(function (BOM) {
    var isIE = !(! BOM.document.attachEvent);

    BOM.CB_getData = function (cType, CallBack) {
        var This = this,
            EM = isIE ?
                    ['attachEvent', 'onpaste', 'detachEvent'] :
                    ['addEventListener', 'paste', 'removeEventListener'];

        if (cType && (! isIE))  switch (cType.toLowerCase()) {
            case 'text':    cType = 'text/plain';    break;
            case 'url':     cType = 'text/unicode';  break;
            case 'html':    cType = 'text/html';     break;
        }
        This.document[ EM[cType ? 0 : 2] ](EM[1], function () {
            var PO = isIE ? This : arguments[0];
            var CBR = CallBack.call(
                isIE ? PO.srcElement : PO.target,
                PO.clipboardData.getData(cType)
            );
            if (! CBR)
                if (isIE)  PO.event.returnValue = false;
                else PO.preventDefault();
        }, isIE ? undefined : false);
    };
})(self);


(function (BOM) {

    function All_Frames(iWindow) {
        var _Frames_ = [].slice.call(iWindow.frames, 0);
        for (var i = 0; i < _Frames_.length; i++)
            _Frames_ = _Frames_.concat( arguments.callee(_Frames_[i]) );
        return _Frames_;
    }

    BOM.Frame_Each = function (CallBack) {
        var Frames = [this].concat( All_Frames(this) );
        for (var i = 0, CBR; i < Frames.length; i++) {
            CBR = CallBack.apply(Frames[i],  [].slice.call(arguments, 1));
            if (CBR === false) break;
            else if (CBR === undefined) continue;
            return CBR;
        }
    };

})(self);


(function (BOM) {

    BOM.HTML_Inject = function (iHTML_Fragment) {
        var  Target_Tag = BOM.Frame_Each(function () {
            try {
                var  This_Tag = this.document.activeElement;
            } catch (iError) { return; }
            if (! This_Tag)  return false;
            switch (This_Tag.tagName.toLowerCase()) {
                case 'body':      ;
                case 'input':     ;
                case 'select':    return;
                case 'iframe':    {
                    if (This_Tag.contentWindow.frames.length) return;
                    This_Tag = This_Tag.contentWindow.document.getElementsByTagName('body')[0];
                }
            }
            return This_Tag;
        });
        if (! Target_Tag) {
            BOM.alert("未找到可用的插入点……\n\n请在要插入代码的编辑框中单击鼠标，再点击本工具~");
            return false;
        }
        Target_Tag.innerHTML = iHTML_Fragment || BOM.prompt("请输入要插入的 HTML 代码片段");
        if (Target_Tag.innerHTML) return;
        if (! Target_Tag.iWatch) {
            BOM.CB_getData.call(
                Target_Tag.ownerDocument.defaultView,
                'Text', function (iHTML) {
                    Target_Tag.innerHTML = iHTML;
                    return false;   
                }
            );
            Target_Tag.iWatch = true;
        }
        BOM.alert("本次没有输入，但之后您可以直接在目标编辑框中粘贴（无需再次点击本工具）~");
    };

    BOM.HTML_Inject();

})(self);